#!usr/bin/env python
#coding=utf8

import re
import os

from django.core.exceptions import ObjectDoesNotExist
from django.db.models import Q

from tuangou import config
from tuangou.models.tuan_models import Item, ItemImage, TuanGouInfo, ItemCategory, ItemLinkItemCategory, ItemLinkSupplier
from tuangou.common_logic import commutil
    
def get_item_list(page, page_size, order_by='-createdtime'):
    page, page_size, offset = commutil.get_page_offset(page, page_size)
    items = Item.objects.order_by(order_by)[offset : offset + page_size]
    for item in items:
        item.price = commutil.decimalFormat(item.price)
        item.tuanprice = commutil.decimalFormat(item.tuanprice)
        try:
            item.homeimage = item.itemimage_set.filter(imagetype='home')[0]  #ItemImage.objects.get(item_id=item.id, imagetype='home')
        except:
            item.homeimage = ItemImage(thumburl='', imagetype='home', originalurl='')
    return items

def get_item_count():
    return Item.objects.count()

def search_item_list(keyword, page, page_size, order_by):
    page, page_size, offset = commutil.get_page_offset(page, page_size)
    items = Item.objects.filter(title__contains=keyword)[offset : offset + page_size]
    for item in items:
        item.price = commutil.decimalFormat(item.price)
        item.tuanprice = commutil.decimalFormat(item.tuanprice)
        try:
            item.homeimage = item.itemimage_set.filter(imagetype='home')[0]  #ItemImage.objects.get(item_id=item.id, imagetype='home')
        except:
            item.homeimage = ItemImage(thumburl='', imagetype='home', originalurl='')
    return items

def search_item_count(keyword):    
    return Item.objects.filter(title__contains=keyword).count()
    
def get_item_by_id(id):
    item = Item.objects.get(id=id)
    item.price = commutil.decimalFormat(item.price)
    item.tuanprice = commutil.decimalFormat(item.tuanprice)
    try:
        item.homeimage = item.itemimage_set.filter(imagetype='home')[0]  #ItemImage.objects.get(item_id=item.id, imagetype='home')
    except:
        item.homeimage = ItemImage(thumburl='', imagetype='home', originalurl='')
    
    #item.tuangouinfos = TuanGouInfo.objects.get(item_id=id)
    
    return item

def del_item_by_id(id):
    '''
    删除商品信息
    '''
    item = Item.objects.get(id=id)
    for img in item.itemimage_set.all():
        path = os.path.join(config.STATIC_PATH, 'productimgs', img.thumburl)
        if os.path.isfile(path):
            os.remove(path)
        path = os.path.join(config.STATIC_PATH, 'productimgs', img.originalurl)
        if os.path.isfile(path):
            os.remove(path)
    item.itemimage_set.all().delete()
    item.tuangouinfo_set.all().delete()

    #删除商品和分类的关系
    ItemLinkItemCategory.objects.filter(item=item.id).delete()
    #删除商品和供应商关系
    ItemLinkSupplier.objects.filter(item=item.id).delete()
    item.delete()

def del_item_image_by_id(image_id):
    img = ItemImage.objects.get(id=image_id)
    path = os.path.join(config.STATIC_PATH, 'productimgs', img.thumburl)
    if os.path.isfile(path):
        os.remove(path)
    path = os.path.join(config.STATIC_PATH, 'productimgs', img.originalurl)
    if os.path.isfile(path):
        os.remove(path)
    img.delete()
 
def save_item(**kwargs):
    item = Item(**kwargs)
    item.save()
    return item

def update_item(item_id, **kwargs):
    item = get_item_by_id(item_id)
    if item:
        item.update(**kwargs)
    return item

def save_item_image(item_id, thumburl, originalurl, imagetype):
    img = ItemImage()
    img.originalurl = originalurl
    img.thumburl = thumburl
    img.item_id = item_id
    img.imagetype = imagetype
    img.save()
    return img

def add_item_categorys(item, category_ids):
    '''
    添加商品分类
    @param category_ids: 用逗分隔的id列表
    '''
    ItemLinkItemCategory.objects.filter(item=item.id).delete()
    ids = category_ids.split(',')
    for id in ids:
        if id:
            cm = ItemLinkItemCategory()
            cm.item_id = item.id
            cm.itemcategory_id = id
            cm.save()

def add_item_supplys(item, supply_ids):
    '''
    添加商品供应商
    @param supply_ids: 用逗分隔的id列表
    '''
    ItemLinkSupplier.objects.filter(item=item.id).delete()
    ids = supply_ids.split(',')
    for id in ids:
        if id:
            cm = ItemLinkSupplier()
            cm.item_id = item.id
            cm.supplier_id = id
            cm.save()            